package oop.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class Group1Project6
{

	public static void main(String[] args)
	{
		ArrayList<String> list1 = new ArrayList<String>();
		ArrayList<String> list2 = new ArrayList<String>();
		
		list1.add("GREG");
		list1.add("CRAIG");
		list1.add("DAVID");
		list1.add("BEHVEAN");
		list1.add("SCOTT");
		
		list2.add("KIM");
		list2.add("BEN");
		list2.add("TIM");
		list2.add("SCOTT");
		list2.add("GARY");

		HashSet<String> unique = uniqueCombineLists(list1, list2);
		
		Iterator it = unique.iterator();
		System.out.println("The Uniique list:");
		System.out.print("[");
		while(it.hasNext())
		{
			System.out.print(it.next());
			if(it.hasNext())
			{
				System.out.print(", ");
			}
		}
		System.out.println("]");
		
		HashSet<String> similar = similarItems(list1, list2);
		
		it = similar.iterator();
		
		System.out.println("The Similar list:");
		System.out.print("[");
		while(it.hasNext())
		{
			System.out.print(it.next());
			if(it.hasNext())
			{
				System.out.print(", ");
			}
		}
		System.out.println("]");
		
		HashSet<String> uncommon = uncommonItems(unique, similar);
		
		it = similar.iterator();
		
		System.out.println("The Uncommon list:");
		System.out.print("[");
		while(it.hasNext())
		{
			System.out.print(it.next());
			if(it.hasNext())
			{
				System.out.print(", ");
			}
		}
		System.out.println("]");
		
		HashMap<String, Integer> map1 = new HashMap<String, Integer>();
		map1.put("GREG", 27);
		map1.put("CRAIG", 40);
		map1.put("DAVID", 42);
		map1.put("BEHVEAN", 45);
		map1.put("SCOTT", 50);
		
		HashMap<String, Integer> map2 = new HashMap<String, Integer>();
		map2.put("KIM", 40);
		map2.put("BEN", 32);
		map2.put("TIM", 50);
		map2.put("SCOTT", 33);
		map2.put("GARY", 70);
		
		HashMap<String, Integer> overwrite = hashMap1(map1,map2);
		
		System.out.println(overwrite);
		//System.out.println(hashMap2(map1,map2));
		
		HashMap<String, Integer> notOverwrite = hashMap2(map1,map2);
		System.out.println(notOverwrite);
		
		HashMap<String, Integer> notOverwriteTake2 = checkKey(map1,map2);
		System.out.println(notOverwriteTake2);
		
		HashMap<String, Integer> map3 = new HashMap<String, Integer>();
		map3.put("Scott", 19);
		
		HashMap<String, Integer> notOverwriteAnotherScott = hashMap2(notOverwrite,map3);
		System.out.println(notOverwriteAnotherScott);
		
		HashMap<String, Integer> notOverwriteAnotherScott2 = checkKey(notOverwrite,map3);
		System.out.println(notOverwriteAnotherScott2);
		
		HashMap<String, Integer> hm1 = new HashMap<String,Integer>();
		hm1.put("GREG", 27);
		System.out.println(hm1);
		HashMap hm2 = new HashMap<String,Integer>();
		hm2.put("SCOTT", 30);
		hm2.put(new Object(), 90);
		System.out.println(hm2);
		HashMap<String, Integer> hm3 = new HashMap();
		hm3.put("JIM", 33);
		System.out.println(hm3);
		hm2 = hm3;
		hm2.put(new Object(), 90);
		System.out.println(hm3);
		
	}
	
	
	static public HashSet<String> uniqueCombineLists(ArrayList<String> list1, ArrayList<String> list2)
	{
		HashSet<String> unique = new HashSet<String>();
		
		unique.addAll(list1);
		unique.addAll(list2);
		
		return unique;
	}
	
	static public HashSet<String> similarItems(ArrayList<String> list1, ArrayList<String> list2)
	{
		HashSet<String> similar = new HashSet<String>();
		
		similar.addAll(list1);
		similar.retainAll(list2);
		
		return similar;
		
	}
	
	static public HashSet<String> uncommonItems(HashSet<String> unique, HashSet<String> similar)
	{
		HashSet<String> uncommon = new HashSet<String>();
		
		uncommon.addAll(unique);
		uncommon.removeAll(similar);
		
		return uncommon;
	}
	
	static public HashMap<String, Integer> hashMap1(HashMap<String, Integer> map1, HashMap<String, Integer> map2)
	{
		HashMap<String, Integer> combinedMap = new HashMap<String, Integer>();
		combinedMap.putAll(map1);
		combinedMap.putAll(map2);
		return combinedMap;
	}
	
	static public HashMap<String, Integer> hashMap2(HashMap<String, Integer> map1, HashMap<String, Integer> map2)
	{
		HashMap<String, Integer> combinedMap1 = new HashMap<String, Integer>();
		combinedMap1.putAll(map1);
//		Set set = combinedMap1.entrySet();
		Iterator<Map.Entry<String, Integer>> it = map2.entrySet().iterator();
		while (it.hasNext())
		{
			Map.Entry<String, Integer> current = it.next();
			String newkey = current.getKey();
			while (combinedMap1.containsKey(newkey))
			{
				newkey = newkey + "1";
			}
			combinedMap1.put(newkey, current.getValue());			
		}
		return combinedMap1;		
	}
	public static HashMap<String, Integer> checkKey(HashMap<String, Integer> map1, HashMap<String, Integer> map2)
	{
		HashMap<String, Integer> map3 = new HashMap<String, Integer> ();
		
		map3.putAll(map1);
		Set<String> set1 = map3.keySet();

		Iterator<String> it = set1.iterator();
		while(it.hasNext())
		{
			String next;
			next = it.next();
			while(map2.containsKey(next) && next != null)
			{
				int tempVal = map2.get(next);
				map2.remove(next);
				String tempKey= next + "1";
				map2.put(tempKey, tempVal);
				System.out.println("The key that has been changed is " + next);
			}
		}
		map3.putAll(map2);
		return map3;
	}
	

}
